package ratons.racleurs.main;

import data.Data_Loader;
import data.UserPreference;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class ParametresActivity extends Activity {

	public UserPreference preferences = UserPreference.getInstance();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_parametres);
		
		final CheckBox chbAll = (CheckBox) findViewById(R.id.checkBoxAll);
		final CheckBox chbEdifices = (CheckBox) findViewById(R.id.checkBoxEdifices);
		final CheckBox chbPlaces = (CheckBox) findViewById(R.id.checkBoxPlaces);
				
		Button btnValider = (Button) findViewById(R.id.buttonParametres);
		
		if(preferences.hasParams(Data_Loader.EDIFICE)&& preferences.hasParams(Data_Loader.PLACE)){
			chbAll.setChecked(true);
		} else if(preferences.hasParams(Data_Loader.EDIFICE)) {
			chbEdifices.setChecked(true);
		} else {
			chbPlaces.setChecked(true);
		}
		
		btnValider.setOnClickListener(new OnClickListener(){

			public void onClick(View arg0) {
				
				//UserPreference preferences = UserPreference.getInstance();
				
				String test = "true" ;
				
				if(chbAll.isChecked())
				{
					preferences.addParams(Data_Loader.EDIFICE);
					preferences.addParams(Data_Loader.PLACE);
				}
				else if(chbEdifices.isChecked() && chbPlaces.isChecked())
				{
					preferences.addParams(Data_Loader.EDIFICE);
					preferences.addParams(Data_Loader.PLACE);
				} 
				else if(chbEdifices.isChecked())
				{
					preferences.addParams(Data_Loader.EDIFICE);
					preferences.removeParams(Data_Loader.PLACE);
				}
				else if(chbPlaces.isChecked()){
					preferences.addParams(Data_Loader.PLACE);
					preferences.removeParams(Data_Loader.EDIFICE);
				} 
				else 
				{
					test = "false";
					Toast.makeText(ParametresActivity.this, R.string.selectionParam, 3).show();
				}
						
				if(test.equals("true")){
					Intent intent = new Intent(ParametresActivity.this, ChoixActivity.class);

		    		startActivity(intent);
				}
				
			}
			
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_parametres, menu);
		return true;
	}

}
